/*
    jME Terra package. A Terrain Engine for the Java Monkey Engine.
    Copyright (C) 2005-2006,  Tijl Houtbeckers
    
    **** LICENSE: ****

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation,
	version 2.1.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

	**** CONTACT: ****

    I can be reached through the jmonkeyengine.com forums (user "llama"), 
    or the jmonkeyengine.com contact page:
    jmonkeyengine.com/index.php?option=com_contact&task=view&contact_id=5           

*/

/**
 * @author Tijl Houtbeckers
 */

package org.llama.jmex.terra.format;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Properties;
import java.util.zip.Deflater;

import org.llama.jmex.terra.HeightMapManager;
import org.llama.jmex.terra.util.TerraBuilder;

public class HdrTerraBuilder extends TerraBuilder {
	
	public HdrTerraBuilder(String[] a) {
		build(a);
	}

	public static void main(String a[]) {
		new HdrTerraBuilder(a);
	}

	public void build(String filename, String pre_in, String pre_out, int blocksize, int mapmulti) throws IOException {
		
		Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(pre_in+filename+".hdr"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        int rowz = Integer.parseInt(properties.getProperty("number_of_rows"));
        int colz = Integer.parseInt(properties.getProperty("number_of_columns"));
        
        
        FileInputStream fis = new FileInputStream(pre_in+filename+".bin");
        DataInputStream dis = new DataInputStream(fis);
        short in = 0;
        short[][] map = new short[colz][rowz];
        int x = 0; int y = 0;
        while (in != Short.MIN_VALUE) {
            in = dis.readShort();
            
            if (in < -10)
                in = 0;
            else 
                in+=10;
            
            map[x++][y] = in;
           
            if (x == map.length) { 
                x = 0;
                y++;
                if (y % ( rowz / 10) == 0)
                    System.out.print(y+"/"+rowz+":");
                if (y == map[x].length)
                    in = Short.MIN_VALUE;
            }
        }
        HeightMapManager hm = new HeightMapManager(blocksize*mapmulti);
        MemoryMappedFileMapStore store = new MemoryMappedFileMapStore(pre_out+filename,true, ".h.terra", false, false);
        ZLibMapCompressor comp = new ZLibMapCompressor(Deflater.BEST_COMPRESSION,Deflater.DEFAULT_STRATEGY);
        
        int msize = hm.getMapSize();
        
        int yl = (rowz / 2) / msize;       
        int xl = (colz / 2) / msize;       
        
        for (y = -yl; y < yl; y++) {
            for (x = -xl; x < xl; x++) {
                int[] hmap = new int[msize * msize];
                int k = 0;
                for (int col = (y+yl) * msize; col < (y+yl+1) * msize; col++) {
                    for (int row = (x+xl) * msize; row < (x+xl+1) * msize; row++) {
//                        if (x == 0 && y==0) {
//                        System.out.println("row: "+row);
//                        System.out.println("col: "+col);
//                        System.out.println("k: "+k);
//                        System.out.println("kl: "+hmap.length);
//                        System.out.println("pos: "+k+" val: "+map[row][col]);
//                        
//                        }
                        if (col >= colz || row >= rowz)
                        	hmap[k++] = 0;
                        else
                        	hmap[k++] = map[col][row];
                    }
                }
                System.out.println("mapfile made: "+x+":"+y);
                ByteBuffer buf = ByteBuffer.allocate(hmap.length * 4);
                buf.asIntBuffer().put(hmap);
                hm.addMap(x, y, buf);
                hm.addCompressedMapStore(x, y, store);
                hm.saveCompressedMapToStore(x, y, store, true, false, false, false, null, comp );
                System.out.println("mapfile saved: "+x+":"+y);

            }
        }
        
        System.out.println("Done");
    }

}
